import Vue from 'vue'
import Vuex from 'vuex'
import service from '@service/httpRequest.js'
import systemProfile from '@store/systemProfile.js'
import userProfile from '@store/userProfile.js'
import component from '@store/component.js'
import webSocket from '@store/webSocket.js'
import constant from '@store/constant.js'

Vue.use(Vuex)

const defaultState = {
    authentication: {
        data: [],
        flag: true,
        state: null,
        text: null,
        mode: 'cockpit',
        homeFlag: false
    },
    // systemInfo: JSON.parse(localStorage.getItem('systemInfo')) || [],
    // functionRouter: JSON.parse(localStorage.getItem('functionRouter'))||{},
    systemInfo:  [],
    functionRouter: {},
    service: {}
}

export default new Vuex.Store({
    namespaced: true,
    modules: {
        systemProfile,
        userProfile,
        component,
        webSocket,
        constant
    },
    state: {
        ...defaultState
    },
    getters: {
        systemInfo: state => {
            return state.systemInfo.length > 0 ? (state.systemInfo.find(item => item.flag) || state.systemInfo[0]) : null
        },
        router: state => {
            return Object.keys(state.component.component).map(key => ({ path: `/${key}`, name: `${key}`, flag: true, component: state.component.component[key] }))
        }
    },
    mutations: {
        updateSystemInfo(state, payload = []) {
            state.systemInfo = payload
            // localStorage.setItem('systemInfo', JSON.stringify(payload))
        },
        updateAuthentication(state, payload = {}) {
            let authenticationObj =  {
                ...defaultState.authentication,
                ...payload
            }
            state.authentication = authenticationObj
            // localStorage.setItem('authentication', JSON.stringify(authenticationObj))
        },
        updateFunctionRouter(state, payload = {}) {
            let functionRouterObj = {
                ...defaultState.functionRouter,
                ...payload
            }
            state.functionRouter = functionRouterObj
            // localStorage.setItem('functionRouter', JSON.stringify(functionRouterObj))
        },
        updateService(state, payload = {}) {
            let serviceObj = {
                ...defaultState.service,
                ...payload
            }
            state.service = serviceObj
            // localStorage.setItem('service', JSON.stringify(serviceObj))
        }
    },
    actions: {
        async init({ dispatch, state }) {
            await dispatch('getService')
            if (state.authentication.flag) {
                await dispatch('component/init')
                // await dispatch('systemProfile/init')
                await dispatch('userProfile/init')
                dispatch('webSocket/init')
            }
        },
        async getSystemInfo({ commit }, data) {
            const result = await service.getSystemInfo(data)
            if (result && result.flag) {
                await commit('updateSystemInfo', result.data)
            }
        },
        async signOut({ commit }) {
            await commit('updateAuthentication', defaultState.authentication)
            service.signOut()
        },
        async signIn({ commit, dispatch, state }, { id, password, mode, homeFlag }) {
            if (state.authentication.flag) {
                await commit('updateAuthentication', { ...state.authentication, mode, homeFlag })
            } else {
                const result = await service.signIn(id, password)
                await commit('updateAuthentication', { ...result, mode, homeFlag })
                if (result.flag) {
                    await dispatch('init')
                }
            }
        },
        async getService({ commit }) {
            const result = await service.getFunction()
            if (result.flag) {
                const functionRouter = {}
                const fetchService = {}
                for (const item of result.data) {
                    functionRouter[item.name] = item.id
                    fetchService[item.name] = async function (...data) {
                        return service.fetchByString(item.id, JSON.stringify(data))
                    }
                }
                await commit('updateFunctionRouter', functionRouter)
                await commit('updateService', { ...service, ...fetchService })
            } else {
                await commit('updateAuthentication', result)
            }
        },
        async uploadFile({ commit, state }, { data, file }) {
            return service.uploadFile(state.functionRouter.importCiConfig, data, file)
        },
        async createCiConfig({ commit, state }, { data, file }) {
            return service.uploadResource(state.functionRouter.addCiConfig, data, file)
        },
        async updateCiConfig({ commit, state }, { data, file }) {
            return service.uploadResource(state.functionRouter.updateCiConfig, data, file)
        }
    }
})